// 课程管理-分类管理
app.controller('courseSortManage',['$scope','$modal','$http','$rootScope','$state', function($scope,$modal,$http,$rootScope,$state){
	$scope.getList();
	
	//添加分类
	$scope.addSort=function(){
		var modalInstance = $modal.open({
		    templateUrl : 'addSort.html',  //指向创建的视图
		    controller : 'addSort',// 初始化模态范围
		    size : "sm",
		    backdrop:"static"
		});

		modalInstance.result.then(function() {
			$scope.getList();
		}, function(){
			$scope.getList();
		});
	}

	//编辑分类
	$scope.editSort=function(item){
		var modalInstance = $modal.open({
		    templateUrl : 'editSort.html',  //指向创建的视图
		    controller : 'editSort',// 初始化模态范围
		    size : "sm",
		    backdrop:"static"
		});

		modalInstance.result.then(function() {
			$scope.getList();
		}, function(){
			$scope.getList();
		});

		$rootScope.editInfo=item;
	}

	// 删除分类
	$scope.deleteSort=function(item){
		swal({
		    title: "确定删除该分类？",
		    type: "warning",
		    confirmButtonColor: "#639DDF",
		    confirmButtonText: "确定",
		    closeOnConfirm: false,
		    showCancelButton: true
		},
		function() {
			$http({
			    url:$rootScope.baseUrl+"Course/delCourseTheme",
			    method:"post",
			    data:{
			    	id:item.id
			    }
			}).then(function(res){
			    if(res.data.code==200){
			    	swal("删除成功", "", "success");
			    	$scope.getList();
			    }else if(res.data.code==3005){
			    	sweetAlert("删除失败",res.data.msg, "error");
			    }else if(res.data.code==409){
			    	$state.go("access.signin");
			    }
			},function(error){
			    console.log(error);
			});
		});
	}
}]);

// 添加分类
app.controller('addSort',['$scope','$http','$rootScope','$modalInstance','$state', function($scope,$http,$rootScope,$modalInstance,$state){
	// 取消
	$scope.cancelAdd=function(){
		$modalInstance.close();
	}

	$scope.imgList=[
		{
			name:"时间题"
		},
		{
			name:"距离题"
		},
		{
			name:"罚款题"
		},
		{
			name:"速度题"
		},
		{
			name:"标线题"
		},
		{
			name:"标志题"
		},
		{
			name:"计分题"
		},
		{
			name:"手势题"
		},
		{
			name:"信号题"
		},
		{
			name:"酒驾题"
		},
		{
			name:"灯光题"
		},
		{
			name:"仪表题"
		},
		{
			name:"装置题"
		},
		{
			name:"路况题"
		}
	];

	$scope.selectIcon=function(index){
		$scope.imgNum=index+1;
	}

	// 确定
	$scope.sureAdd=function(){
		if($scope.name==undefined || $scope.imgNum==undefined){
			swal("请填写完整的信息");
		}else {
			$http({
			    url:$rootScope.baseUrl+"Course/addCourseTheme",
			    method:"post",
			    data:{
			    	name:$scope.name,
			    	img_num:$scope.imgNum
			    }
			}).then(function(res){
			    if(res.data.code==200){
			    	swal({
			    	    title: "添加成功",
			    	    type: "success",
			    	    showCancelButton: false,
			    	    confirmButtonColor: "#619ce1",
			    	    confirmButtonText: "确定",
			    	    closeOnConfirm: true
			    	},
			    	function() {
			    	    $modalInstance.close();
			    	});
			    }else if(res.data.code==3009){
			    	swal(res.data.msg);
			    }else if(res.data.code==409){
			    	$state.go("access.signin");
			    }
			},function(error){
			    console.log(error);
			});
		}
	}
}]);

// 编辑分类
app.controller('editSort',['$scope','$http','$rootScope','$modalInstance', '$state',function($scope,$http,$rootScope,$modalInstance,$state){
	$scope.editInfo=$rootScope.editInfo;
	// 取消
	$scope.cancelAdd=function(){
		$modalInstance.close();
	}

	$scope.imgList=[
		{
			name:"时间题"
		},
		{
			name:"距离题"
		},
		{
			name:"罚款题"
		},
		{
			name:"速度题"
		},
		{
			name:"标线题"
		},
		{
			name:"标志题"
		},
		{
			name:"计分题"
		},
		{
			name:"手势题"
		},
		{
			name:"信号题"
		},
		{
			name:"酒驾题"
		},
		{
			name:"灯光题"
		},
		{
			name:"仪表题"
		},
		{
			name:"装置题"
		},
		{
			name:"路况题"
		}
	];

	$scope.selectIcon=function(index){
		$scope.editInfo.img_num=index+1;
	}

	// 确定
	$scope.sureAdd=function(){
		if($scope.editInfo.name==undefined || $scope.editInfo.img_num==undefined){
			swal("请填写完整的信息");
		}else {
			$http({
			    url:$rootScope.baseUrl+"Course/editCourseTheme",
			    method:"post",
			    data:{
			    	id:$scope.editInfo.id,
			    	name:$scope.editInfo.name,
			    	img_num:$scope.editInfo.img_num
			    }
			}).then(function(res){
			    if(res.data.code==200){
			    	swal({
			    	    title: "编辑成功",
			    	    type: "success",
			    	    showCancelButton: false,
			    	    confirmButtonColor: "#619ce1",
			    	    confirmButtonText: "确定",
			    	    closeOnConfirm: true
			    	},
			    	function() {
			    	    $modalInstance.close();
			    	});
			    }else if(res.data.code==3009){
			    	swal(res.data.msg);
			    }else if(res.data.code==409){
			    	$state.go("access.signin");
			    }
			},function(error){
			    console.log(error);
			});
		}
	}
}]);